import {Body, Controller, Get, Headers, HttpStatus, Post, Query, Res} from '@nestjs/common';
import {Response} from 'express';
import {CommonResponseDto} from '../dto/common.response.dto';
import {AppStatus, ResCode} from '../common/constants';
import {AppListService} from '../service/app.list.service';
import {formatTime, uuid} from "../common/utils";
import {decrypt, encrypt, generateRandomTokenBase64, getModelConfigList} from "../common/common";
import {AppListDto} from "../dto/app.list.dto";


@Controller('api/app')
export class AppListController {
    constructor(private readonly appListService: AppListService) {}

    @Get('/list')
    async getAllList(@Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        try {
            const data = await this.appListService.getAllAppList()
            return new CommonResponseDto(ResCode.OK, 'success', data)
        } catch(e){
            return new CommonResponseDto(ResCode.ERROR, '获取数据异常')
        }
    }

    @Get('/get')
    async getOne(@Query('appId') appId: string, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        try {
            const data = await this.appListService.getOneByAppId(appId)
            return new CommonResponseDto(ResCode.OK, 'success', data)
        } catch(e){
            return new CommonResponseDto(ResCode.ERROR, '获取数据异常')
        }
    }

    @Post('/create')
    async createOne(@Body() params: {remark: string}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        try {
            const time = new Date().getTime()
            const createDate = formatTime(time)
            const appDto: AppListDto = {
                appId: uuid(16),
                appSecret: generateRandomTokenBase64(32),
                status: AppStatus.NORMAL,
                remark: params.remark || '',
                createDate
            }
            await this.appListService.createOne(appDto)
            return new CommonResponseDto(ResCode.OK, 'success', appDto)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '创建异常')
        }
    }

    @Post('/status-update')
    async updateStatus(@Body() params: {appId: string, status: AppStatus,}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        if (![AppStatus.NORMAL, AppStatus.DELETED, AppStatus.LOCKED].includes(params.status)) {
            return new CommonResponseDto(ResCode.ERROR, '状态值设置错误')
        }
        if (!params.appId) {
            return new CommonResponseDto(ResCode.ERROR, '缺少参数')
        }
        try {
            const result = await this.appListService.editStatus(params.appId, params.status)
            if (result) {
                return new CommonResponseDto(ResCode.OK, 'success')
            }
            return new CommonResponseDto(ResCode.ERROR, '更新失败')
        } catch(e){
            return new CommonResponseDto(ResCode.ERROR, '更新异常')
        }
    }
}
